Ext.ns('Wlanauth.role');

Wlanauth.role.FormPanel = Ext.extend(Wlanauth.BaseFormPanel,{
	resourcePanel:null,
	constructor:function(config){
		Ext.apply(this,config);
		
		var myCheckboxGroup = new Ext.form.FieldSet({
		    title: '系统常规权限',
		    itemCls: 'x-check-group-alt',
		    // Put all controls in a single column with width 100%
		    defaults:{xtype:'checkbox'},
		    layout:'column',
		    items:[{
		    	boxLabel:'查看'
		    },{
		    	boxLabel:'添加'
		    },{
		    	boxLabel:'修改'
		    },{
		    	boxLabel:'删除'
		    }]
		});
	 	this.resourcePanel = new Ext.Panel({
	 		border:false
	 	});
	 	Wlanauth.role.FormPanel.superclass.constructor.call(this,{
			actionClass:'role',
			buttonAlign:'center',
			waitMsgTarget: true,
	        padding:5,
	        defaults:{xtype:'textfield'},
	        items:[{
	        	fieldLabel:'编码',
	        	width:250,
	        	name:'entity.code',
	        	allowBlank:true,
	        	xtype:'hidden'
	        },{
	        	width:250,
	        	fieldLabel:'名称',
	        	name:'entity.name',
	        	allowBlank:false
	        },{
	        	fieldLabel:'资源',
	        	xtype:'panel',
	        	border:false,
	        	anchor:'90%',
	        	items:this.resourcePanel
	        },{
	        	fieldLabel:'备注',
	        	xtype:'textarea',
	        	name:'entity.description',
	        	anchor:'90%'
	        },{
	        	xtype:'hidden',
	        	name:'entity.id'
	        },{
	        	xtype:'hidden',
	        	name:'entity.editable'
	        }]
     	});
	 	this.addEvents('resourceComponentsInited');
	 	this.initPanel();
	},
	initPanel:function(){
		var ownerCt = this.resourcePanel;
		//
		function renderData(rows,ownerCt){
			var index = 0;
			for(var i=0;i<rows.length;i++){
				var node = rows[i];
				
				var items = [];
				if(!node.leaf){
					for(var j=0;j<node.children.length;j++){
						var cnode = node.children[j];
						items.push({
							boxLabel:cnode.text,
							inputValue:cnode.id,
							name:'systemResources['+index+'].id'
						});
						index++;
					}
				}
				
				var myCheckboxGroup = new Ext.form.FieldSet({
				    title: node.text,
				    itemCls: 'x-check-group-alt',
				    // Put all controls in a single column with width 100%
				    defaults:{xtype:'checkbox'},
				    layout:'column',
				    items:items
				});
				ownerCt.add(myCheckboxGroup);
				ownerCt.doLayout();
				
			}
		}
		Ext.Ajax.request({
			url:'systemResource!getSystemResourceTree.action',
			scope:this,
			success:function(response,opts){
				var rs = Ext.decode(response.responseText);
				if(rs.success&&rs.rows){
					renderData(rs.rows,ownerCt);
					this.fireEvent('resourceComponentsInited',this);
				}
			}
		});
	},
	isValid:function(form){
      	var flag = form.isValid();
      	if(flag){
      		var editable = form.findField('entity.editable').getValue();
      		if(this.editMode!='create'&&(!editable||editable=='false')){
      			Ext.Msg.alert('提示','该角色不可编辑');
      			return false;
      		}
      	}
      	return flag;
    },
	onLoadDataSuccess:function(form,action){
		var systemResources = action.result.data.systemResources;
		var checkboxs = this.findByType('checkbox');
		for(var i=0;i<checkboxs.length;i++){
			var flag = false;
			var cb = checkboxs[i];
			for(var j=0;j<systemResources.length;j++){
				if(systemResources[j].code=='.*'||systemResources[j].id==cb.inputValue){
					flag=true;
					break;
				}
			}
			if(flag){
				cb.setValue(true);
			} else {
				cb.setValue(false);
			}
		}
	}
});

Ext.reg('wlanauth.role.formpanel',Wlanauth.role.FormPanel);